Using other than standard Persistence Mappers per attribute

In MDriven, it is possible to create your own persistence mappers per attribute. If you do not create your own, it is still possible to pick and choose from the ones supplied. Note that this level of specialization will stop the generic tools like MDriven Prototyping and MDriven Server from functioning properly (they use our standard).

One specific case that raises questions about this functionality is Enums.

Our standard is to store the Enum value as its text representation in the database.

However, legacy databases typically treat application-level Enums as integers.

This question was once posted on the forum:

When using enums that are persisted with the GenericEnumAsInteger mapper, you can’t execute a query like this:

MyEnumType eTest = MyEnumType.theFirstValue
IList<Class1> result = (from x in ecoSpace.PSQuery<Class1>() where
(x.MyEnumAttribute == eTest) select x).ToList();

In this case, you get the following error:

An exception of type ‘System.Data.SqlClient.SqlException’ occurred in Eco.Persistence.dll but was not handled in the user code.

Additional information: Error converting a nvarchar value to int. Using the GenericEnumAsVar or executing a MemQuery instead works as expected.

What the questioner (Alois) has done in this case is to override the suggested persistence mapper on the attribute level:

Persistencce Mapper 1.png

The name GenericEnumAsInteger was found in this collection:

Persistencce Mapper 2.png

Persistencce Mapper 3.png

What the questioner was unaware of – since this was not easily understood – hence this article – is how MDriven finds what enum persistence mapper to use. For the attribute, we use the override value set on the attribute, but for the parameter, the attribute is not available to us.

Instead, we use this strategy:

  1. Try to find a Persistence Mapper with the same name as the Type of the parameter. In this case “EcoProject1.MyEnumType”
  2. If none was found and if the parameter is of type Enum – we try to find the persistence mapper with the name “Enum”

This is what happens in this particular case. In the error reported, I believe that the “Enum” persistence mapper is still set to “Eco.Persistence.Default.GenericEnumAsVarChar”.

Persistencce Mapper 4.png

What must be done to make the LINQ query below work is either to change the persistence mapper Enum to Eco.Persistence.Default.GenericEnumAsInteger or add a new row to the collection of persistence mappers named EcoProject1.MyEnumType with MapperTypeName Eco.Persistence.Default.GenericEnumAsInteger .

MyEnumType eTest = MyEnumType.theFirstValue
IList<Class1> result = (from x in ecoSpace.PSQuery<Class1>() where (x.MyEnumAttribute
== eTest) select x).ToList();
This page was edited 85 days ago on 08/28/2024. What links here